Component({
  properties: {

  },
  data: {
    top: 320,
    left: 580,
    startX: 0,
    startY: 0
  },
  lifetimes: {
    // attached() {
    //   // 获取上次保存的位置
    //   const top = wx.getStorageSync('buttonTop') || 400;
    //   const left = wx.getStorageSync('buttonLeft') || 520;
    //   this.setData({ top, left });
    // }
  },
  methods: {
    toCustom(){
      wx.navigateTo({
        url:'/subpackages/custom/doing/index?type=offlineCustom',
      })
    },
    onTouchStart(e) {
      this.setData({
        startX: e.touches[0].clientX - this.data.left * wx.getSystemInfoSync().windowWidth / 750,
        startY: e.touches[0].clientY - this.data.top * wx.getSystemInfoSync().windowHeight / 750
      });
    },
    onTouchMove(e) {
      const windowWidth = wx.getSystemInfoSync().windowWidth;
      const windowHeight = wx.getSystemInfoSync().windowHeight;
      const rpxToPx = windowWidth / 750; // 750rpx = 1px

      const x = e.touches[0].clientX - this.data.startX;
      const y = e.touches[0].clientY - this.data.startY;

      // 限制按钮的移动范围
      const newTop = Math.max(0, Math.min(y / rpxToPx, windowHeight - 100));
      const newLeft = Math.max(0, Math.min(x / rpxToPx, windowWidth - 200));

      this.setData({
        top: newTop,
        left: newLeft
      });

      // 保存按钮位置到本地存储
      wx.setStorageSync('buttonTop', newTop);
      wx.setStorageSync('buttonLeft', newLeft);
    }
  }
});
